package cn.assumejson.pro_netty.netty.client;

import cn.assumejson.pro_netty.netty.client.asyn.RequestFuture;
import cn.assumejson.pro_netty.netty.client.asyn.Response;
import cn.assumejson.pro_netty.register.ServerChangeWatcher;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.Promise;
import org.slf4j.Logger;

import static cn.assumejson.pro_netty.utils.GsonUtil.gson;

/**
 * @author by gang.Li 2022/3/12 11:52
 */
@ChannelHandler.Sharable
public class ClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        Response response = JSONObject.parseObject(msg.toString(), Response.class);
//        gson.toJson(msg.toString(), Response.class);
        Response response = gson.fromJson(msg.toString(), Response.class);
        // 将结果返回主线程，并且唤醒主线程
        RequestFuture.received(response);
        // 设置响应结果并唤醒主线程
//        promise.setSuccess(response);
    }
}
